package com.bto.solar.work.service;

import com.bto.solar.work.common.BizException;
import com.bto.solar.work.constant.Constants;
import com.bto.solar.work.dao.ICustomerService;
import com.bto.solar.work.dao.IDesignService;
import com.bto.solar.work.dao.IDeviceService;
import com.bto.solar.work.dao.IOrderBaseService;
import com.bto.solar.work.dao.IStoDeviceService;
import com.bto.solar.work.dto.BtoStoResp;
import com.bto.solar.work.entity.*;
import com.bto.solar.work.utils.NullUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@Service
@Slf4j
public class BtoStoService {

    @Autowired
    private IOrderBaseService orderBaseService;
    @Autowired
    private ICustomerService customerService;
    @Autowired
    private IDesignService designService;
    @Autowired
    private IDeviceService deviceService;
    @Autowired
    private BtoCompanyService btoCompanyService;
    @Autowired
    private IStoDeviceService stoDeviceService;


    /**
    * 根据设备编码，查询设备信息
    */
    public List<StoDevice> getStoDeviceByCodes(List<String> codeList){
        if(NullUtils.isEmpty(codeList))
            return Collections.emptyList();
        return stoDeviceService.lambdaQuery()
                .in(StoDevice::getDeviceId, codeList).list();
    }


    public void updateStoDevicesStoIdToOrderId(List<String> deviceCodeList , String orderId){
        stoDeviceService.lambdaUpdate()
                .set(StoDevice::getStoId , orderId)
                .in(StoDevice::getDeviceId , deviceCodeList).update();
    }





    public BtoStoResp getBtoStoRespByOrderId (String orderId) {
        BtoStoResp btoStoResp = new BtoStoResp();
        OrderBase orderBase = orderBaseService.getById(orderId);//
        if (null == orderBase) {
            throw new BizException("订单不存在");
        }
        btoStoResp.setOrderId(orderId);//系统编号
        btoStoResp.setSalesman(orderBase.getSalesman());//销售人员
        btoStoResp.setSaleTelephone(orderBase.getSaleTelephone());//销售手机号
        btoStoResp.setDetailedAddress(orderBase.getInstallationAddress());//详细地址
        btoStoResp.setLng(orderBase.getLng());//经度
        btoStoResp.setLat(orderBase.getLat());//纬度


        Company company;
        if (NullUtils.isNotNull(orderBase.getCompanyId())) {
            company = btoCompanyService.getCompanyById(orderBase.getCompanyId());
            btoStoResp.setCompanyName(company.getName());//所在组织
            btoStoResp.setCompanyType(company.getType());//公司类型
        } else {
            company = btoCompanyService.getCompanyByUserId(orderBase.getUserId());
            btoStoResp.setCompanyName(company.getName());//所在组织
            btoStoResp.setCompanyType(company.getType());//公司类型
        }

        Customer customer = customerService.getById(orderBase.getCustomerId());
        if (null == customer) {
            throw new BizException("订单中客户不存在");
        }
        btoStoResp.setCustomerNmae(customer.getName());//客户姓名
        btoStoResp.setCustomerPhone(customer.getPhone());//客户手机号

        Design design = designService.lambdaQuery()
                .eq(Design::getOrderId, orderBase.getOrderId())
                .one();
        if (null == design) {
            throw new BizException("订单中设计信息不存在");
        }
        btoStoResp.setInstalledCapacityDesign(design.getInstalledCapacityDesign());//设计容量
        List<String> deviceTypeList = Arrays.asList(Constants.DEVICE_TYPE_ZUJIAN, Constants.DEVICE_TYPE_NBQ,
                Constants.DEVICE_TYPE_CJQ, Constants.DEVICE_TYPE_PDX);
        List<Device> deviceList = deviceService.lambdaQuery()
                .eq(Device::getOrderId, orderBase.getOrderId())
                .in(Device::getDeviceType, deviceTypeList).list();
        if (deviceList.size() > 0) {
            btoStoResp.setDeviceList(deviceList);
        } else {
            throw new BizException("订单中设备信息不存在");
        }

        return btoStoResp;
    }

}
